// 自定义js
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

const user_key = 'user_info';
// 把字符串变为元素
function Yy_stringToElement(htmlString) {
    var parser = new DOMParser();
    var doc = parser.parseFromString(htmlString, "text/html");
    return doc.body.firstChild; // 返回body的第一个子节点  
}

// 判断本地是否存储了用户
function YyIsLogin() {
    const YyUserInfo = localStorage.getItem(user_key);
    if (YyUserInfo == null) {
        // window.location.href = '/login/'
        return false
    } else {
        return true;
    }
}

// 读取本地内容: 默认读取用户本地
function YyGetLocatStorge(keyName) {
    if (keyName == undefined) {
        keyName = user_key;
    }
    const YyUserInfo = localStorage.getItem(keyName);
    if (YyUserInfo == null) {
        return YyUserInfo;
    } else {
        return JSON.parse(YyUserInfo);
    }

}

// 设置本地
function YySetLocatStorge(keyName, data) {
    if (keyName == undefined) {
        keyName = user_key;
    }
    localStorage.setItem(keyName, JSON.stringify(data));
}

// 清除本地缓存
function YyRemoveItem(key) {
    if (key == undefined) {
        localStorage.clear();
    } else {
        localStorage.removeItem(key);
    }
}
// 返回上一级
function YyBack() {
    window.history.back();
}


// 获取地址栏参数
function YygetQueryParam(name) {
    var params = new URLSearchParams(window.location.search);
    return params.get(name);
}


// 请求
async function YyAxios(Objects) {
    /**
     * method、url、data
     */
    if (Objects.method == 'GET') {
        let response = await axios({
            url: Objects.url,
            method: 'GET',
            params: Objects.data
        })
        return response.data;
    } else {
        let response = await axios({
            url: Objects.url,
            method: 'POST',
            data: Objects.data
        })
        return response.data;

    }
    return response.data;
}

// 刷新网页
function YyRefresh() {
    window.location.reload();
}

// 跳转网页
function YyGo(url) {
    window.location.href = url;
}


// 重置表单
function YyFormReset(doc) {
    // doc: 表单元素
    document.querySelector(doc).reset();
}


// 比较字典是否相同
function YydeepEqual(obj1, obj2) {
    if (obj1 === obj2) {
        return true;
    }

    if (typeof obj1 !== 'object' || obj1 === null ||
        typeof obj2 !== 'object' || obj2 === null) {
        return false;
    }

    const keys1 = Object.keys(obj1);
    const keys2 = Object.keys(obj2);

    if (keys1.length !== keys2.length) {
        return false;
    }

    for (let key of keys1) {
        if (!keys2.includes(key) || !YydeepEqual(obj1[key], obj2[key])) {
            return false;
        }
    }

    return true;
}

//   判断当前数据在列表中是否存在一模一样的数据
function YyhasSameElements(arr, element) {
    return arr.includes(element.replace(/\s/g, '').trim());
}


// 海角解密JS
function YyHaiJiao() {
    var e = "ABCD*EFGHIJKLMNOPQRSTUVWX#YZabcdefghijklmnopqrstuvwxyz1234567890"
        , t = (this.encode = function (i) {
            var a, n, o, r, s, c, l = "", d = 0;
            for (i = t(i); d < i.length;)
                o = (a = i.charCodeAt(d++)) >> 2,
                    r = (3 & a) << 4 | (a = i.charCodeAt(d++)) >> 4,
                    s = (15 & a) << 2 | (n = i.charCodeAt(d++)) >> 6,
                    c = 63 & n,
                    isNaN(a) ? s = c = 64 : isNaN(n) && (c = 64),
                    l = l + e.charAt(o) + e.charAt(r) + e.charAt(s) + e.charAt(c);
            return l
        }
            ,
            this.decode = function (t) {
                var a, n, o, r, s, c, l = "", d = 0;
                for (t = t.replace(/[^A-Za-z0-9\*\#]/g, ""); d < t.length;)
                    o = e.indexOf(t.charAt(d++)),
                        a = (15 & (r = e.indexOf(t.charAt(d++)))) << 4 | (s = e.indexOf(t.charAt(d++))) >> 2,
                        n = (3 & s) << 6 | (c = e.indexOf(t.charAt(d++))),
                        l += String.fromCharCode(o << 2 | r >> 4),
                        64 != s && (l += String.fromCharCode(a)),
                        64 != c && (l += String.fromCharCode(n));
                return i(l)
            }
            ,
            function (e) {
                e = e.replace(/\r\n/g, "\n");
                for (var t = "", i = 0; i < e.length; i++) {
                    var a = e.charCodeAt(i);
                    a < 128 ? t += String.fromCharCode(a) : t = 127 < a && a < 2048 ? (t += String.fromCharCode(a >> 6 | 192)) + String.fromCharCode(63 & a | 128) : (t = (t += String.fromCharCode(a >> 12 | 224)) + String.fromCharCode(a >> 6 & 63 | 128)) + String.fromCharCode(63 & a | 128)
                }
                return t
            }
        )
        , i = function (e) {
            for (var t, i, a = "", n = 0, o = 0; n < e.length;)
                (t = e.charCodeAt(n)) < 128 ? (a += String.fromCharCode(t),
                    n++) : 191 < t && t < 224 ? (o = e.charCodeAt(n + 1),
                        a += String.fromCharCode((31 & t) << 6 | 63 & o),
                        n += 2) : (o = e.charCodeAt(n + 1),
                            i = e.charCodeAt(n + 2),
                            a += String.fromCharCode((15 & t) << 12 | (63 & o) << 6 | 63 & i),
                            n += 3);
            return a
        }
}


/*字符串解析成元素节点类型*/
function parseElement(str) {
	var o=document.createElement("div");
	o.innerHTML=str;
	return o.childNodes[0];
}


// 去除全部空格
function YyremoveSpaces(str) {
    return str.replace(/\s+/g, '');
  }